<?php
include_once('Mail.php');
include_once('MailDecoder.php');
include_once('PopServer.php');

class MailReceiver{
	private $popServer;
	private $error;
	private $state=false;
 
	public function MailReceiver($server,$username,$password,$port=110){
		
		$this->popServer = new PopServer($server,$username,$password,$port,5,0);
		$this->state = false;
	}
	
	/**
	 * 打开连接
	 */
	public function open(){
		if(!$this->popServer->open()){
			$this->error = $this->popServer->__getErr_str();
			return false;
		}
		if(!$this->popServer->Login()){
			$this->error = $this->popServer->__getErr_str();
			return false;
		}
		$this->state = true;
		return true;
	}
	/**
	 * 关闭连接
	 */
	public function close(){		
		$this->popServer->close();
	}
	/**
	 * 根据邮件编号获取最新邮件数（该编号以后的邮件）
	 * @param unknown_type $latest_num
	 * @return -1:有异常，其他：邮件数
	 */
	public function getNew_mail_count($latest_num=0){
		if(!$this->state){
			$this->error = "服务器未连接！";
			return -1;
		}
		//获取总有邮件数
		if(!$this->popServer->stat()){
			$this->error = $this->popServer->__getErr_str();
			return -1;
		}
		$total_count = $this->popServer->__getMail_count();
		if($total_count<=0) return 0;
		//获取所有邮件列表摘要信息
		if(!$this->popServer->listmail()){
			$this->error = $this->popServer->__getErr_str();
			return -1;
		}
		$total_mail=$this->popServer->__getMail_list();
		//获取最新邮件数
		$new_count = 0;
		foreach($total_mail as $tm){
			if($tm[num]<=$latest_num) continue;
			$new_count++;
		}
		return $new_count;
	}
	
	/**
	 * 根据邮件编号获取最新邮件列表（该编号以后的邮件） 
	 * @param int $latest_num
	 * @return array('编号1','编号2',......)，* 若 -1:有异常
	 */
	public function getLatest_mail_list($latest_num=0){
		if(!$this->state){
			$this->error = "服务器未连接！";
			return -1;
		}
		//获取总有邮件数
		if(!$this->popServer->stat()){
			$this->error = $this->popServer->__getErr_str();
			return -1;
		}
		$total_count = $this->popServer->__getMail_count();
		if($total_count<=0) return null;
		//获取所有邮件列表摘要信息
		if(!$this->popServer->listmail()){
			$this->error = $this->popServer->__getErr_str();
			return -1;
		}
		$total_mail=$this->popServer->__getMail_list();
		//获取最新邮件数
		$new_mail_list = array();
		$i=0;
		foreach($total_mail as $tm){
			if($tm[num]<=$latest_num) continue;
			$new_mail_list[$i][num]=$tm[num];
			$new_mail_list[$i][size]=$tm[size];
			$i++;
		}
		return $new_mail_list;
	}
	
	/**
	 * 根据编号获取邮件内容
	 * @param int $num
	 */
	public function getMail_by_num($num,$des_attachment_fold,$des_charset="UTF-8"){
		$decoder = new MailDecoder();
		$mail = new Mail();
		if(!$this->popServer->getmail($num)){
			$this->error = $this->popServer->__getErr_str();
			return null;
		}
		
		$head = $this->popServer->__getHead();
		$body = $this->popServer->__getBody();
		
		$decoder->decode($head,$body);
		$decoder->change_charset($des_charset);//////////////
		
		
		$mail->__setNum($num);
		$mail->__setSubject($decoder->subject);
		$mail->__setFrom($decoder->from_mail);
		$mail->__setFrom_name($decoder->from_name);
		$mail->__setTo($decoder->to_mail);
		$mail->__setTo_name($decoder->to_name);
		$mail->__setCc($decoder->cc_to);
		$mail->__setDate(date("Y-m-j h:m:s",$decoder->mail_time));
		
		$size = 0;				//邮件大小
		$attachment = array();	//附件数组
		$content_plain = '';	//文本内容
		$content_html = '';	//html内容
		
		$i=0;
		for ($j=0;$j<count($decoder->body);$j++)
		{
			if($decoder->body[$j][type]=='text/plain'){
				$content_plain=$decoder->body[$j][content];
			}elseif($decoder->body[$j][type]=='text/html'){
				$content_html=$decoder->body[$j][content];
			}else{
				if($decoder->body[$j][isattachment]=='1'){
					$filename = $decoder->body[$j][name];
					if(empty($filename)||$filename=='')	$filename = $decoder->body[$j][filename];
					if($filename==''){
						continue;
					}
					
					$fp=fopen($des_attachment_fold.$filename,"w");
					fwrite($fp,$decoder->body[$j][content]);
					fclose($fp);
					//附件信息
					$attachment[$i][path]= $des_attachment_fold.$decoder->body[$j][name];
					$attachment[$i][size]= $decoder->body[$j][size];
					$i++;
				}
			}
			$size = $size+$decoder->body[$j][size];
		}
		if(empty($content_html)||$content_html=='') $content_html = $content_plain;
		$mail->__setSize($size);
		$mail->__setContent($content_html);
		$mail->__setAttachment($attachment);
		
		return $mail;
	}
	
	
	public function getError(){
		return $this->error;
	}
	
	
}
?>